/**
 * User: yuhao
 * Date: 15-5-14
 * Time: 下午5:43
 * UI管理器，负责系统总体的UI调度
 */

define(
    [
        "require",
        "dojo/_base/declare",
        "dojo/_base/lang",
        "dojo/_base/array",
        "dojo/dom",
        "dojo/dom-attr",
        "dojo/dom-class",
        "dojo/dom-style",
        "dojo/_base/html",
        "dojo/window",
        "dojo/on",
        "dojo/fx",
        "dojo/query",
        "dojo/topic",
        "dojo/_base/fx",
        "dojo/date/locale",
        "esri/symbols/PictureMarkerSymbol",
        "esri/renderers/ClassBreaksRenderer",
        "./Map/Map",
        "../gistech/util/utils",
        "../gistech/util/ajaxUtil",
        "dijit/layout/ContentPane",
        "../gistech/widgets/ZoomBar/ZoomBar",
        "../gistech/widgets/ArrowWidget/ArrowWidget",
        "../gistech/widgets/ToolBox/ToolBox",
        "../gistech/widgets/menubarWidget/MenuBarWidget",
        "../gistech/widgets/rightWidget/RightWidget",
        "../gistech/widgets/leftWidget/LeftWidget",
        "../gistech/widgets/FeatureQueryWidget/FeatureQueryWidget",
        "../gistech/widgets/UserSet/UserSet",
        "../gistech/widgets/MapLayerResourceWidget/MapLayerResourceWidget"
    ],
    function (require, declare, lang, array, dom, domAttr, domClass, 
    		domStyle, html, win, on,dojofx, query, topic,fx,locale,PictureMarkerSymbol,
    		ClassBreaksRenderer,Map, utils,ajaxUtil,ContentPane,
    		ZoomBar,ArrowWidget,ToolBox,MenuBarWidget,RightWidget,LeftWidget,FeatureQueryWidget,UserSet,MapLayerResourceWidget ) {

        var instance = null, clazz;
       // "../gistech/widgets/leftWidget/LeftWidget",
        clazz = declare(null, {

            constructor:function (args) {
                this.widgets = {};	// 引用的组件     
                this.map=null;
                this.sysManageMenu = false;
                
            },

            /**
             * 初始化系统UI
             */
            init:function () {
                this.startMonitor();

                //初始化容器
                var box = win.getBox(),
                    wrapper = dom.byId("wrapper");
                domStyle.set(wrapper, "width", box.w + "px");
                domStyle.set(wrapper, "height", box.h + "px");
                this.loadMap();
            },

            
            /**
             * 加载组件
             */
            loadWidgets:function (map) {
            	
            	/*菜单栏*/
            	var menubarwidget = new MenuBarWidget({
                     map:map,
                },dojo.byId("menubarDiv"));
            	menubarwidget.startup();
            	
            	/*左侧导航栏*/
            	var leftWidget = new LeftWidget({relate:".mainArea"},dojo.byId("leftTabCon"));
            	leftWidget.startup();
            	this.initLeftWidget();
            	
            	/*右侧导航栏*/
            	var rightWidget = new RightWidget({},dojo.byId("rightTabCon"));
            	rightWidget.startup();
            	
            	/*搜索框*/
            	var querybarwidget = new FeatureQueryWidget({},dojo.byId("querybar"));
            	querybarwidget.startup();
            	
                /*放大缩小组件*/
                var zoombar=ZoomBar({
                	map:map
                });
                zoombar.placeAt(dojo.body());
                zoombar.startup();
                this.widgets["ZoomBar"] = zoombar;
                
                /*右下角工具条arr*/
                var bottomArrow2=new ArrowWidget({
                    right:'15px',
                    bottom:'105px',
                    expandState:'top',
                    eventName:'toggleToolBox',
                    id:"bottomArrow2"  
                });
                bottomArrow2.startup();
                bottomArrow2.placeAt(dojo.body());
                this.widgets["BottomArrow2"] = bottomArrow2;
                
                /*工具条组件*/
                var toolbox=ToolBox({
                	  map:map,
                	  container:dojo.body()
                });
                toolbox.startup();
                this.widgets["ToolBox"] = toolbox;
                
        
                
                /*框选组件*/
//                var extendQueryWidget = new ExtendQueryWidget({
//                     map:map,
//                },dojo.byId("extendQueryWidgetDiv"));
//                extendQueryWidget.startup();
//                this.widgets["ExtendQueryWidget"] = extendQueryWidget;
  
//                /*框选组件*/
//                var sysManageMenuWidget = new SysManageMenuWidget({
//                     map:map,
//                },dojo.byId("sysmanagemenu"));
//                sysManageMenuWidget.startup();
 
            },
            initLeftWidget:function(){
            	var lefttabCon = dijit.byId("leftTabCon");
            	
            	var ResourceTab = new  ContentPane();

            	ResourceTab.set('content', "<div id='ResourceTab' ></div>");
            	lefttabCon.addTab(ResourceTab,"资源列表",false);
            	
            	lefttabCon.setWidth();
            	var mapLayerResourceWidget = new MapLayerResourceWidget({map:this.map,lefttabCon:this.lefttabCon},dojo.byId("ResourceTab"));
            	mapLayerResourceWidget.startup();
            	
            	var CityTab = new  ContentPane();

            	CityTab.set('content', "<div id='CityTab' ></div>");
            	lefttabCon.addTab(CityTab,"城市列表",false);
            	lefttabCon.selectTab(ResourceTab);
            	
            },
            /**
             * 开始监听消息
             */
            startMonitor:function () {
                topic.subscribe("mapLoaded", lang.hitch(this, this.handleMapLoaded));
 
                on(window, "resize", function () {
                    topic.publish("windowResize");
                });

                on(document.body, "click", function (e) {
                    topic.publish("bodyClick", e);
                });

                on(document.body, "keyup", function (e) {
                    topic.publish("bodyKeyup", e);
                });
                

                on(dojo.byId("username"),"click",function(e){
                	if(this.sysManageMenu){
                		dojofx.wipeOut({node: "sysmanagemenu"}).play();
    					this.sysManageMenu = false;
    				}
    			    else{//列表已显示，则收起列表
    			    	dojofx.wipeIn({node: "sysmanagemenu"}).play();
    					this.sysManageMenu = true;
    				}
                });
                on(dojo.byId("sysmanagepage"),"click",function(e){
                	var userSet = new UserSet({pos:{l:Math.ceil(document.body.clientWidth)/2-419,t:110}},dojo.byId("dialog"));
                	userSet.setTitle("人员维护");
                	userSet.startup();
                });
            },

            /**
             * 加载地图并初始化widgets
             */
            loadMap:function () {
                var mapWidget = new Map({
                    mapId:"map"
                });
                mapWidget.startup();
                this.widgets["map"] = mapWidget;
            },

            /**
             * 地图加载完毕的回调
             * @param map
             */
            handleMapLoaded:function (args) {           	
            	this.map=args.map;
                this.loadWidgets(args.map);
               
            },
 
 
        });
        
        clazz.getInstance = function (args) {
            if (instance === null) {
                instance = new clazz(args);
            }
            return instance;
        };
        return clazz;
    });